import BreathingApertureMarker from './marker/BreathingApertureMarker';
import DiffusedApertureMarker from './marker/DiffusedApertureMarker';
import HaloRingMarker from './marker/HaloRingMarker';
import RotatingApertureMarker from './marker/RotatingApertureMarker';
import RotatingTextBorderMarker from './marker/RotatingTextBorderMarker';
import FluorescenceMarker from './marker/FluorescenceMarker';
export var AnimateMarkerType;
(function (AnimateMarkerType) {
    AnimateMarkerType[AnimateMarkerType["BreathingApertureMarker"] = 0] = "BreathingApertureMarker";
    AnimateMarkerType[AnimateMarkerType["DiffusedApertureMarker"] = 1] = "DiffusedApertureMarker";
    AnimateMarkerType[AnimateMarkerType["HaloRingMarker"] = 2] = "HaloRingMarker";
    AnimateMarkerType[AnimateMarkerType["RotatingApertureMarker"] = 3] = "RotatingApertureMarker";
    AnimateMarkerType[AnimateMarkerType["RotatingTextBorderMarker"] = 4] = "RotatingTextBorderMarker";
    AnimateMarkerType[AnimateMarkerType["FluorescenceMarker"] = 5] = "FluorescenceMarker";
})(AnimateMarkerType || (AnimateMarkerType = {}));

class AnimateMarkerLayer {
  constructor(map, animateMarkerType) {
      if (!map) {
          throw new Error('map is requierd');
      }
      this.map = map;
      this.animateMarkerType = animateMarkerType;
      this.markers = [];
      switch (this.animateMarkerType) {
          case AnimateMarkerType.RotatingApertureMarker:
              this.marker = new RotatingApertureMarker(this.options);
              break;
          case AnimateMarkerType.HaloRingMarker:
              this.marker = new HaloRingMarker(this.options);
              break;
          case AnimateMarkerType.BreathingApertureMarker:
              this.marker = new BreathingApertureMarker(this.options);
              break;
          case AnimateMarkerType.DiffusedApertureMarker:
              this.marker = new DiffusedApertureMarker(this.options);
              break;
          case AnimateMarkerType.RotatingTextBorderMarker:
              this.marker = new RotatingTextBorderMarker(this.options);
              break;
          case AnimateMarkerType.FluorescenceMarker:
              this.marker = new FluorescenceMarker(this.options);
              break;
      }
  }

  setWidth(value) {
      this.marker && this.marker.setMarkersWidth(value);
  }

  setHeight(value) {
      this.marker && this.marker.setMarkersHeight && this.marker.setMarkersHeight(value);
  }

  setTextColor(value) {
      this.marker && this.marker.setMarkersTextColor(value);
  }

  setTextFontSize(value) {
      this.marker && this.marker.setMarkersTextFontSize(value);
  }

  setColors(value) {
      this.marker && this.marker.setMarkersColors(value);
  }

  setTextField(value) {
      this.marker && this.marker.setMarkersTextField(value);
  }

  addFeature(feature) {
      let geometry = feature.geometry;
      if (geometry && geometry.coordinates && geometry.coordinates.length !== 0 && geometry.type === 'Point') {
          this.marker.features.features.push(feature);
      }
      this.marker.setMarkersElement();
      this._initalizeMarkerLayer();
  }

  bindFeatures(features) {
      this.marker.features = features;
      this.marker.setMarkersElement();
      this._initalizeMarkerLayer();
  }

  _initalizeMarkerLayer() {
      if (!this.marker.features || JSON.stringify(this.marker.features) === '{}') {
          return;
      }
      this.clear();
      this._createMarker();
  }

  _createMarker() {
      if (this.marker.markersElement.length === 0) {
          return;
      }
      this.marker.features.features.forEach((point, index) => {
          let coordinates = point.geometry.coordinates;
          if (coordinates) {
              let marker = new mapboxgl.Marker(this.marker.markersElement[index] || this.marker.markersElement[0])
                  .setLngLat(coordinates)
                  .addTo(this.map);
              this.markers.push(marker);
          }
      }, this);
  }

  clear() {
      this.markers.length > 0 &&
          this.markers.forEach(marker => {
              marker && marker.remove();
          });
      this.markers = [];
  }
}

export { AnimateMarkerLayer }
